<?php
namespace app\api\model;



use think\Model;
use think\facade\Request;
class Evaluation extends Model
{
	public function add($data){
		
		foreach ($data['datas'] as $key => $value) {
			
			$ids = [];
		 foreach ($value['img'] as $k => $v) {
	 	$img = ['images' => $v];
	 	$ids[] = db('image')->insertGetId($img);
	 }

	  $ids = implode(",",$ids);
	 $info = array(
      'uid'=>$data['uid'],
      'content'=>$value['content'],
      'goods_id'=>$value['goods_id'],
      'score'=>$value['wjxScore'],
      'img'=>$ids,
      'create_time'=>time()
	 	);
	 $res = db('evaluation')->insert($info);
	 db('goods')->where('id',$value['goods_id'])->setInc('goods_comments',1);
		}
	
	 if($res){
	 	db('order')->where('id',$data['id'])->update(['status'=>6]);

	 	return_json(200,'成功',true);
	 }else{
	 	return_json(400,'失败',false);
	 }
	}

	public function img($id,$comment=''){
		if(empty($comment)){
			 $comment = db('evaluation')->where('goods_id',$id)->limit(1)->order('id desc')->select();
			}
		
         foreach ($comment as $k => $v) {
        	$v['img']=explode(",",$v['img']);
              $comment[$k]['img'] = array();
        
        	   foreach ($v['img'] as $key => $value) {
               $img = db('image')->field('images')->where('id', $value)->find();
          
              $comment[$k]['img'][]=$img['images'];
            
            }
              $comment[$k]['user'] = db('user')->field('vip,avatar_url,nick_name')->where('id',$v['uid'])->find();
        }

    
        
        return $comment;
        
	}

	public function sel($goods_id){
		 $comment = db('evaluation')->where('goods_id',$goods_id)->order('create_time desc')->select();
        $res = $this->img($goods_id,$comment);
        return_json(200,'成功',$res);
	}
}



?>